"""
Celery应用配置
用于爬虫任务的异步执行和调度
"""
from celery import Celery
from kombu import Exchange, Queue
import os
from typing import Any, Dict

# 创建Celery应用实例
app = Celery('crawler_scheduler')

# 加载配置
app.config_from_object('src.celery_config')

# 定义任务队列
app.conf.task_routes = {
    'crawler.tasks.*': {'queue': 'crawler_queue'},
    'scheduler.tasks.*': {'queue': 'scheduler_queue'},
    'health.tasks.*': {'queue': 'health_queue'},
}

# 定义优先级队列
app.conf.task_queue_max_priority = 10
app.conf.task_default_priority = 5

# 配置任务队列
app.conf.task_queues = (
    Queue('high_priority', Exchange('high'), routing_key='high', priority=10),
    Queue('normal_priority', Exchange('normal'), routing_key='normal', priority=5),
    Queue('low_priority', Exchange('low'), routing_key='low', priority=1),
    Queue('crawler_queue', Exchange('crawler'), routing_key='crawler.*'),
    Queue('scheduler_queue', Exchange('scheduler'), routing_key='scheduler.*'),
    Queue('health_queue', Exchange('health'), routing_key='health.*'),
)

# 自动发现任务
app.autodiscover_tasks(['src.tasks'])

@app.task(bind=True)
def debug_task(self):
    """调试任务"""
    print(f'Request: {self.request!r}')
    return 'Debug task executed successfully'

if __name__ == '__main__':
    app.start()